<?php //strip
/**
 * Implementation of the model for catalog categories.
 * 
 * @package odCatalogWpPlugin
 * @subpackage odCatalogWpPlugin_Models
 * @author Ondrej Donek <ondrejd@gmail.com>
 */
class odCatalogWpPlugin_Models_Category extends odWpPlugin_Model_Simple
{
	public function __construct()
	{
		global $wpdb;
		
		$this->mTableName = $wpdb->prefix . 'catalog_categories';
		$this->mPrimary = 'ID';
		$this->mCheckColumn = 'title';
		$this->mColumns = array(
			'ID' => array('type' => 'int', 'primary' => true),
			'parent_ID' => array('type' => 'int', 'default' => 0),
			'locale' => array('type' => 'char', 'length' => 5, 'notnull' => true, 'default' => 'en'),
			'urlid' => array('type' => 'char', 'length' => 75, 'notnull' => true),
			'title' => array('type' => 'char', 'length' => 75, 'notnull' => true),
			'description' => array('type' => 'text', 'notnull' => true, 'default' => ''),
			'deleted' => array('type' => 'tinyint', 'length' => 1, 'notnull' => true, 'default' => 0)
		);
	}
	
	public function get($aFilter = '', $aCount = 0, $aLimitFrom = 0)
	{
		global $wpdb;
		
		$query = 'SELECT ' . PHP_EOL . 
				'  `t1`.`ID` AS `ID` , ' . PHP_EOL . 
				'  `t1`.`parent_ID` AS `parent_ID` , ' . PHP_EOL . 
				'  `t2`.`title` AS `parent_title` , ' . PHP_EOL . 
				'  `t1`.`locale` AS `locale` , ' . PHP_EOL . 
				'  `t1`.`urlid` AS `urlid` , ' . PHP_EOL . 
				'  `t1`.`title` AS `title` , ' . PHP_EOL . 
				'  `t1`.`description` AS `description` , ' . PHP_EOL . 
				'  `t1`.`deleted` AS `deleted` ' . PHP_EOL . 
				'FROM `' . $this->mTableName . '` AS `t1` ' . PHP_EOL . 
				'LEFT JOIN `' . $this->mTableName . '` AS `t2` ON `t2`.`ID` = `t1`.`parent_ID` ' . PHP_EOL . 
				'WHERE ' . PHP_EOL . $aFilter . PHP_EOL;
		
		if($aLimitFrom > 0 && $aCount > 0) {
			$query .= 'LIMIT ' . $aLimitFrom . ', ' . $aCount . ' ;';
		}
		else if($aCount > 0) {
			$query .= 'LIMIT ' . $aCount . ' ;';
		}
		
		return $wpdb->get_results($query, ARRAY_A);
	}
	
	/**
	 * Returns categories as a tree.
	 *
	 * @todo Cache result options!
	 * @todo Use current locales!
	 * @todo This should be located directly in odWpPlugin_Model - which should
	 *       keep common properties for all tree-structures.
	 * 
	 * @param integer $aParentId Optional. Defaultly 0.
	 * @param string $aLocale Optional. Defaultly 'en'.
	 * @param boolean $aAlsoDeleted Optional. Defaultly 'false'.
	 * @return array
	 */
	public function getAsTree($aParentId = 0, $aLocale = 'en', $aAlsoDeleted = false)
	{
		// TODO Normalize $aLocale!
		$filter = '`t1`.`parent_ID` = ' . (int) $aParentId . ' ';
		$filter .= ((!$aAlsoDeleted === true) ? 'AND `t1`.`deleted` = 0 ' : '');
		
		$filter .= 'AND `t1`.`locale` = \'' . $aLocale . '\' ';
		$categories = $this->get($filter);
		$tree = array();
		
		if(!is_null($categories)) {
			foreach($categories as $category) {
				$category['children'] = $this->getAsTree($category['ID'],
														 $aLocale,
														 $aAlsoDeleted);
				array_push($tree, $category);
			}
		}
		
		return $tree;
	}
	
} // End of odCatalogWpPlugin_Models_Category
